home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!panix!usenet
- From: traub@panix.com (Amy Weintraub)
- Newsgroups: comp.lang.c++
- Subject: Re: need help with linklists
- Date: 4 Mar 1996 03:47:29 GMT
- Organization: PANIX Public Access Internet and Unix, NYC
- Message-ID: <4hdp4h$sim@news1.panix.com>
- References: <4hd3qn$qo6@ixnews2.ix.netcom.com>
- NNTP-Posting-Host: traub.dialup.access.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.2
-
- In article <4hd3qn$qo6@ixnews2.ix.netcom.com>, wells2@ix.netcom.com says...
- >
- >
- >
- >does anyone have a simple code that does a linklist??
-
- >thanks
- >edward
- >
- > wells2@ix.netcom.com
- >
-
- Edward,
-
- I just wrote this simple program in C that uses a linked list. The program reads a file or
- keyboard input from stdin and keeps track of how many times each word occurs then prints a
- report one word per line saying how many times each word occurred. Each word goes in a
- node. It probably isn't fully optimized, I'm only a second semester C student. But, it
- works, and it is simple. I hope this helps.
-
- /* wclist.c -- reads a file or keyboard input from stdin and keeps
- * track of how many times each word occurs
- * then prints a report one word per line
- * Amy Weintraub 2/1/96 - Data Structures homework 2 */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "wclist.h"
-
- int main(int argc, char *argv[])
- {
- char *currword = NULL; /* pointer to current word */
- struct node *head = NULL; /* set empty list to point to NULL */
- /* get a word and put it in a node */
- while((currword = getword()) != NULL) {
- findlink(&head, currword);
- }
-
- /* print list - should print words with number of occurrences */
- printlist(head);
-
- return 0;
- }
- /* wc.c -- gets a word from stdin put it in a buffer and return
- * NULL when done
- * getword()
- * Amy Weintraub DS hw3 */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include "wclist.h"
-
- #define TRUE 1
- #define FALSE 0
- char * getword()
- {
- char c;
- char wordbuf[MAXWORD];
- int wordflag = FALSE; /* TRUE when in a word */
- int i = 0; /* index for wordbuf */
-
- while((c = getchar()) != EOF) {
- if(c != '\n' && c != ' ' && c != '\t' && wordflag == FALSE){
- wordflag = TRUE; /* start new word */
- /* maybe add char checking for punctuation */
- *(wordbuf + i) = c; /* fill wordbuf with chars */
- i++;
- }
-
- else if(c != '\n' && c != ' ' && c != '\t' &&
- wordflag == TRUE){
- *(wordbuf + i) =c;
- i++;
- }
-
- if((c == ' ' || c == '\n' || c == '\t') &&
- wordflag == TRUE) {
-
- wordflag = FALSE; /* end of word */
- *(wordbuf + i) = '\0'; /* null term array */
-
- /* return to main() to link it in */
- return wordbuf; /* returns local var */
- }
- }
- return NULL; /* to show EOF reached by getword from stdin */
- }
-
- /* wclinked.c -- functions for linked list
- * printlist(), errorfunc(), findlink(),
- * Amy Weintraub Data Structures */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "wclist.h"
- /* printlist: visit each node in list and print it out */
- void printlist(struct node *head)
- {
- struct node *pnode = head;
-
- while(pnode) {
- printf("%6d ", pnode->count);
- printf("%s\n", pnode->data);
- pnode = pnode->next;
- }
- }
- /*************************************************************/
- /* error function: prints message and exits program */
- void errorfunc(char *s)
- {
- fprintf(stderr, "%s", s);
- exit(1);
- }
- /*************************************************************/
- void findlink(struct node **head, char *word)
- {
- struct node *prev = *head;
- struct node *curr = *head;
- struct node *n;
- int cmpval = -1;
-
- while(curr && (cmpval = strcmp(curr->data, word)) < 0) {
- prev = curr;
- curr = curr->next;
- }
-
- /* if a match is found */
- if(cmpval == 0)
- curr->count++;
-
- /* no node exists so make one */
- else {
- if((n = (struct node *)malloc(sizeof(*n))) == NULL)
- errorfunc("Exhauted Memory");
- n->count = 1;
- n->data = strdup(word);
- /* beginning? */
- if((*head == NULL) || (*head == curr)) {
- n->next =curr;
- *head = n;
- }
-
- /* insertion in middle or end */
- else {
- n->next = curr;
- prev->next = n;
- }
- }
- }
-
- /* wclist.h -- header for ll ds homework 2
- * Amy Weintraub */
-
- #define MAXWORD 254 /* max line length in C */
- struct node {
- char *data;
- int count; /* number of times word occurs */
- struct node *next;
- };
- extern void errorfunc(char *s);
- extern void printlist(struct node *head);
- extern void findlink(struct node **head, char *word);
- extern char * getword();
-
-
-
-
-
-